﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour
{

    //控制视野缩放的速率
    //public float view_value;
    //控制摄像机移动的速率
    public float move_speed = 0.03f;
    private Camera m_cam;

    /*旋转镜头参数*/
    //定义枚举数据结构，将名称和设置关联起来
    public enum RotationAxes
    {
        MouseXAndY = 0,
        MouseX = 1,
        MouseY = 2
    }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityHor = 9.0f;//水平旋转的速度
    public float sensitivityVert = 9.0f;//垂直旋转的速度
    public float minimumVertY = -45.0f;//水平旋转的最小角度
    public float maximumVertY = 45.0f;//水平旋转的最大角度
    public float minimumVert = -45.0f;//垂直旋转的最小角度
    public float maximumVert = 45.0f;//垂直旋转的最大角度
    private float _rotationX = 0;//为垂直角度声明一个私有变量
    private float _rotationY = 0;//为水平角度声明一个私有变量

    public bool isRotating;
    // Use this for initialization
    private void Awake()
    {
        //m_cam = this.GetComponent<Camera>();
    }
    void Start()
    {
        m_cam = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        ScrollView();
        RotateView();
        TransView();
    }

    //摄像机左右位置

    void TransView()
    {
        if (Input.GetKey(KeyCode.D))//向右看
        {
            if (this.transform.position.x < 1)
                transform.Translate(Vector3.right * move_speed);
            //Debug.Log("left");
        }
        if (Input.GetKey(KeyCode.A))//向左看
        {
            if (this.transform.position.x > -1)
                transform.Translate(Vector3.left * move_speed);
            //Debug.Log("right");
        }
        if (Input.GetKey(KeyCode.S))//向上看
        {
            if (this.transform.position.y > -0.5)
                this.gameObject.transform.Translate(Vector3.down * move_speed);
            //Debug.Log("up");
        }
        if (Input.GetKey(KeyCode.W))//向下看
        {
            if (this.transform.position.y < 1)
                this.gameObject.transform.Translate(Vector3.up * move_speed);
            //Debug.Log("down");
        }
    }
    //视野缩放
    void ScrollView()
    {
        //放大视野
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (m_cam.fieldOfView <= 50)
            {
                m_cam.fieldOfView += 1;
            }
        }

        //缩小视野
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (m_cam.fieldOfView >= 5)
            {
                m_cam.fieldOfView -= 1;
            }
        }

        //放大、缩小
        /*if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            this.gameObject.transform.Translate(new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * view_value));
            transform.position = new Vector3(0, 1.5f, Mathf.Clamp(transform.position.z, 0, 5));
            
        }*/


    }

    //视野旋转
    void RotateView()
    {
        if (Input.GetMouseButtonDown(2))
        {
            isRotating = true;
        }

        //抬起鼠标右键关闭旋转摄像机
        if (Input.GetMouseButtonUp(2))
        {
            isRotating = false;
        }

        if (isRotating)
        {
            Rotate();
        }

    }

    void Rotate()
    {

        //minimumVert = (5 - transform.position.z) * 9 - 45;
        //maximumVert = -minimumVert;
        //minimumVertY = (5 - transform.position.z) * 9 - 45;
        //maximumVertY = -minimumVert;
        if (axes == RotationAxes.MouseX)
        {
            //水平旋转代码
            _rotationY -= Input.GetAxis("Mouse X") * sensitivityVert;
            _rotationY = Mathf.Clamp(_rotationY, minimumVertY, maximumVertY);
            float rotationX = transform.localEulerAngles.x;//保持x轴与原来一样
            transform.localEulerAngles = new Vector3(rotationX, _rotationY, 0);

        }
        else if (axes == RotationAxes.MouseY)
        {
            //垂直旋转代码
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);

            float rotationY = transform.localEulerAngles.y;//保持y轴与原来一样
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
        else
        {

            //水平且垂直旋转
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);//限制角度大小

            //float delta = Input.GetAxis("Mouse X") * sensitivityHor;//设置水平旋转的变化量
            //float rotationY = transform.localEulerAngles.y + delta;//原来的角度加上变化量

            _rotationY -= Input.GetAxis("Mouse X") * sensitivityVert;
            _rotationY = Mathf.Clamp(_rotationY, minimumVertY, maximumVertY);

            transform.localEulerAngles = new Vector3(_rotationX, _rotationY, 0);//相对于全局坐标空间的角度
        }
    }
}

